
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.GenericsUtils;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Condition;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.db.jdbc.RowMapperFactory;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.constant.PubgConstant;
import com.jf.cloud.service.sysmanager.constant.UserConstant;
import com.jf.cloud.service.sysmanager.dao.IPubgClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgOperateLogService;
import com.jf.cloud.service.sysmanager.dao.IPubgTeamService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserTeamService;
import com.jf.cloud.service.sysmanager.entity.PubgClub;
import com.jf.cloud.service.sysmanager.entity.PubgOperateLog;
import com.jf.cloud.service.sysmanager.entity.PubgTeam;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.PubgUserTeam;


/**
 * 操作记录服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgOperateLogService")
public class PubgOperateLogServiceImpl extends BaseService<PubgOperateLog, Long> implements IPubgOperateLogService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgOperateLogServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	@Resource
	private IPubgClubService pubgClubService;
	
	@Resource
	private IPubgTeamService pubgTeamService;
	
	@Resource
	private IPubgUserClubService pubgUserClubService;
	
	@Resource
	private IPubgUserTeamService pubgUserTeamService;
	
	private RowMapper<PubgOperateLog> rowMapper = new BeanPropertyRowMapper<PubgOperateLog>(GenericsUtils.getSuperClassGenricType(this.getClass()));

	/**
	 * 根据主键查询操作记录
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public PubgOperateLog findPubgOperateLog(Long rowId) throws Exception {
		try {
			return this.baseDao.findObject("", rowMapper, rowId);
		} catch (Exception e) {
			throw new Exception("根据主键查询操作记录出错！", e);
		}
	}

	/**
	 * 根据查询条件获取操作记录列表
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public List<PubgOperateLog> findPubgOperateLogList(Condition qc) throws Exception {
		try {
			return this.baseDao.findList("", rowMapper);
		} catch (Exception e) {
			throw new Exception("根据查询条件获取操作记录列表出错！", e);
		}
	}

	/**
	 * 新增/修改操作记录
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public PubgOperateLog saveOrUpdatePubgOperateLog(PubgOperateLog pubgOperateLog) throws Exception {
		try {
			return this.baseDao.saveAll(pubgOperateLog);
		} catch (Exception e) {
			throw new Exception("新增/修改操作记录出错！", e);
		}
	}

	/**
	 * 插入用户操作记录
	 * 
	 * @author 朱建文 创建时间：2018年3月31日 下午4:53:12
	 */
	@Transactional
	public void insertUserOperateLog(Long userId, Long optType, String content) {
		try {
			
			String sql = "SELECT * FROM pubg_user WHERE ROW_ID = ?";
			PubgUser pubgUser = this.baseDao.findObject(sql, RowMapperFactory.newInstance(PubgUser.class), userId);
			
			PubgOperateLog pubgOperateLog = new PubgOperateLog();
			pubgOperateLog.setOptType(optType);
			pubgOperateLog.setContent(content);
			pubgOperateLog.setUserId(pubgUser.getRowId());
			pubgOperateLog.setUserAccount(pubgUser.getUserAccount());
			pubgOperateLog.setOptTime(DateUtil.getCurrentDateTime());
			pubgOperateLog.setOptId(pubgUser.getRowId());
			pubgOperateLog.setOptName(pubgUser.getUserAccount());
			
			Query query = new Query();
			query.put("USER_ID", pubgUser.getRowId());
			query.put("RELATION_STATE", PubgConstant.USER_TEAM_RELATION_STATE_2);
			
			// 查询操作记录-战队关系 (这块有点问题？？？？  一个人加入了同一个俱乐部的两个战队时 就有问题)
			if (UserConstant.USER_ROLE_10.getId().equals(pubgUser.getUserRole())) {
				List<PubgUserTeam> pubgUserTeamList = pubgUserTeamService.findList(query);
				if (pubgUserTeamList != null && pubgUserTeamList.size() == 1) {
					pubgOperateLog.setTeamId(pubgUserTeamList.get(0).getTeamId());
					pubgOperateLog.setTeamName(pubgUserTeamList.get(0).getTeamName());
					pubgOperateLog.setClubId(pubgUserTeamList.get(0).getClubId());
					pubgOperateLog.setClubName(pubgUserTeamList.get(0).getClubName());
				}
			}
			
			if (UserConstant.USER_ROLE_20.getId().equals(pubgUser.getUserRole())) {
				query = new Query();
				query.put("_LIKE$CAPTAIN_ID", "," + pubgUser.getRowId() + ",");
				List<PubgTeam> pubgTeamList = this.pubgTeamService.findList(query);
				if (pubgTeamList != null && !pubgTeamList.isEmpty()) {
					pubgOperateLog.setTeamId(pubgTeamList.get(0).getRowId());
					pubgOperateLog.setTeamName(pubgTeamList.get(0).getTeamName());
				}
			}
			
			if (UserConstant.USER_ROLE_30.getId().equals(pubgUser.getUserRole())) {
				query = new Query();
				query.put("_LIKE$MINISTER_ID", "," + pubgUser.getRowId() + ",");
				List<PubgClub> pubgClubList = this.pubgClubService.findList(query);
				if (pubgClubList != null && !pubgClubList.isEmpty()) {
					pubgOperateLog.setClubId(pubgClubList.get(0).getRowId());
					pubgOperateLog.setClubName(pubgClubList.get(0).getClubName());
				}
			}
			
			this.saveObj(pubgOperateLog);
		} catch (Exception e) {
			log.error("插入用户操作记录出错！", e);
		}
	}

}
